PHP

PHP operations on the MongoDB[NoSQL] database


1. MongoDB profile

MongoDB (named from “humongous”) is an extensible, high-performance, open source, schema-free, document-oriented database that combines the advantages of document databases, key-value pairs, and relational databases. The official site: http: / / www mongodb. org /, MongoDB features:

The & # 8226; Document-oriented storage (the JSON data schema is simple and powerful) The & # 8226; A dynamic query The & # 8226; Full index support, extended to internal objects and nested arrays The & # 8226; Query record analysis The & # 8226; Quick, in-place updates The & # 8226; Efficient storage of large objects in base 2 (such as photos and videos) The & # 8226; Replication and failover support The & # 8226; Auto-Sharding automatic sharding supports cloud-level scalability The & # 8226; MapReduce supports complex aggregation The & # 8226; Business support, training and consulting 2. Install MongoDB

MongoDB installation is very simple, just download package decompression operation command to download address: http: / / www mongodb. org/downloads, for windows platform, this paper MongoDB run the command: > bin/mongod. Note: to create a folder for storing data, MongoDB defaults to /data/db/ (or c:\data\db). You can also change it to a different directory, just specify —dbpath, eg: > bin/mongod —dbpath=d:\mgdata\db 3. Install the MongoDB PHP extension According to your own PHP version download PHP extension: http: / / www php. net manual/en/mongo installation. # php mongo. installation. windows, tip: 1. VC6 is suitable for Apache, VC9 is suitable for IIS; 2. Thread safe is suitable for PHP to operate in modules, and Non-thread safe is suitable for CGI to operate in modules. Modify php.ini, add: extension= php_mongo.dll, restart Web server. 4. PHP example 1. Connect to Mongo server

<?php 
// The connection localhost:27017 
$conn = new Mongo(); 
// Connect to the remote host's default port  
$conn = new Mongo('test.com'); 
// Connect to remote host 22011 port  
$conn = new Mongo('test.com:22011'); 
//MongoDB Password for user name  
$conn = new Mongo("mongodb://${username}:${password}@localhost");
//MongoDB Password for the useful account name and specify the database blog 
$conn = new Mongo("mongodb://${username}:${password}@localhost/blog"); 
// Multiple servers  
$conn = new Mongo("mongodb://localhost:27017,localhost:27018"); 
?>

2. Specify database and dataset name (table name)

<?php 
// Select database blog 
$db = $conn->blog; 
// Set of results (table name: users )
$collection = $db->users; 
?>
3 , CRUD
<?php 
// new  
$user = array('name' => 'caleng', 'email' => '[email protected]'); 
$collection->insert($user); 
// Modify the  
$newdata = array('$set' => array("email" => "[email protected]")); 
$collection->update(array("name" => "caleng"), $newdata); 
// delete  
$collection->remove(array('name'=>'caleng'), array("justOne" => true)); 
// To find the  
$cursor = $collection->find(); 
var_dump($cursor); 
// To find the 1 article  
$user = $collection->findOne(array('name' => 'caleng'), array('email')); 
var_dump($user); 
?>
4 , close the connection
<?php 
$conn->close(); 
?>